package calorie.com.calorie.view;

import android.app.Dialog;
import android.app.DownloadManager;
import android.content.Context;
import android.content.IntentFilter;
import android.support.v7.widget.AppCompatButton;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.net.ServiceGenerator;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.ToastUtils;

import calorie.com.calorie.R;
import calorie.com.calorie.util.DownloadManagerUtil;
import calorie.com.calorie.util.DownloadReceiver;


public class DownLoadDialogView extends Dialog {
    private String url = ServiceGenerator.BASE_URL+":8089/image/app-release.apk";//较小的apk文件
    private String title = "Calorie";//这里可以写应用名
    private String desc = "一边运动,一遍赚钱";//这里填写一些对应用的介绍
    private boolean isRegisterReceiver;
    TextView resultView;
    AppCompatButton bt_start_download;
    long downloadId = 0;

    public DownLoadDialogView(Context context, int themeResId) {
        super(context, themeResId);
        setReceiver();

//        View view = LayoutInflater.from(context).inflate(R.layout.dialog_download, null);
//        this.setContentView(view);
//        resultView = view.findViewById(R.id.resultView);
//        bt_start_download = view.findViewById(R.id.bt_start_download);
//        bt_start_download.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                DownloadManagerUtil downloadManagerUtil = new DownloadManagerUtil(getContext());
//                if (downloadId != 0) {
//                    downloadManagerUtil.clearCurrentTask(downloadId);
//                }
//                downloadId = downloadManagerUtil.download(url, title, desc);
//                DownLoadDialogView.this.dismiss();
//                ToastUtils.showLongToast(getContext(),"正在努力下载最新的app");
//            }
//        });
    }
    AlertDialog alertDialog = null;
    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext()).addDefaultAnimation().setCancelable(true)
                .setContentView(R.layout.dialog_download)
                .setWidthAndHeight(SizeUtils.dp2px(getContext(),279),SizeUtils.dp2px(getContext(),157))
                .setOnClickListener(R.id.bt_start_download, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DownloadManagerUtil downloadManagerUtil = new DownloadManagerUtil(getContext());
                        if (downloadId != 0) {
                            downloadManagerUtil.clearCurrentTask(downloadId);
                        }
                        downloadId = downloadManagerUtil.download(url, title, desc);
                        DownLoadDialogView.this.dismiss();
                        ToastUtils.showLongToast(getContext(),"正在努力下载最新的app");
                        alertDialog.dismiss();
                    }
                });
        alertDialog = builder.create();
        alertDialog.show();

    }

    @Override
    public void show() {
        showDialog();
    }

    /**
     * 注册下载成功的广播监听
     */
    private void setReceiver() {
        if (!isRegisterReceiver) {
            DownloadReceiver receiver = new DownloadReceiver();
            IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
            getContext().registerReceiver(receiver, intentFilter);
            isRegisterReceiver = true;
        }
    }

}
